--[[ Inspired by yleaf's FogBG located at http://www.wowinterface.com/downloads/info12518-FogBG.html ]]--

--local AuraFrame = CreateFrame("Model", nil, UIParent)
local AuraFrame = CreateFrame("PlayerModel", nil, UIParent)
local config = CreateFrame("Frame")
local height = GetScreenHeight()
local tip = GameTooltip

local defaults = {
	hmult = 1,
	alpha = 1,
}

local function SetUpModel()
	AuraFrame:SetHeight(height * AmbientAuraDB.hmult)  -- place for variable
	AuraFrame:SetAlpha(AmbientAuraDB.alpha)  -- place for variable
end

local function AddOptions()
	--CREATE BLIZZ OPTIONS--
	local header = config:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
		header:SetParent(config)
		header:SetPoint("TOPLEFT", config, "TOPLEFT", 20,-15)
		header:SetText("AmbientAura")
	local subheader = config:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
		subheader:SetParent(config)
		subheader:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0,-10)
		subheader:SetPoint("RIGHT", config, "RIGHT", -20,0)
		subheader:SetHeight(30)
		subheader:SetNonSpaceWrap(true)
		subheader:SetJustifyH("LEFT")
		subheader:SetText("Creates a swirly fog effect along the bottom of your screen, colored by combat/resting status.")
	local hslider = CreateFrame("Slider", nil, config, "OptionsSliderTemplate")
		hslider:SetMinMaxValues(.25,3)
		hslider:SetPoint("TOPLEFT", subheader, "BOTTOMLEFT", 0, -35)
		hslider:SetValueStep(.1)
		hslider:SetValue(AmbientAuraDB.hmult)  -- place for variable (current value)
		hslider:SetScript("OnValueChanged", function(_,val) AmbientAuraDB.hmult = val SetUpModel() end)
		hslider:SetScript("OnEnter", function(self)
					tip:SetOwner(self, "ANCHOR_TOPRIGHT")
					tip:SetText("Set the height of the fog on your screen.")
					tip:Show()
				end)
		hslider:SetScript("OnLeave", function() GameTooltip:Hide() end)
	local htext = hslider:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		htext:SetParent(config)
		htext:SetPoint("BOTTOM", hslider, "TOP", 0,5)
		htext:SetText("Fog Height")
	local aslider = CreateFrame("Slider", nil, config, "OptionsSliderTemplate")
		aslider:SetMinMaxValues(.1,1)
		aslider:SetPoint("LEFT", hslider, "RIGHT", 30, 0)
		aslider:SetValueStep(.05)
		aslider:SetValue(AmbientAuraDB.alpha)  -- place for variable (current value)
		aslider:SetScript("OnValueChanged", function(_,alpha) AmbientAuraDB.alpha = alpha SetUpModel() end)
		aslider:SetScript("OnEnter", function(self)
					tip:SetOwner(self, "ANCHOR_TOPRIGHT")
					tip:SetText("Set the alpha of the fog on your screen.")
					tip:Show()
				end)
		aslider:SetScript("OnLeave", function() GameTooltip:Hide() end)
	local atext = hslider:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		atext:SetParent(config)
		atext:SetPoint("BOTTOM", aslider, "TOP", 0,5)
		atext:SetText("Fog Alpha")
end

local function Initialize()
	--SAVED VARIABLES--
	AmbientAuraDB = AmbientAuraDB or {}
	for k,v in pairs(defaults) do
	    if type(AmbientAuraDB[k]) == "nil" then
	        AmbientAuraDB[k] = v
	    end
	end

	--SLASH COMMANDS / ADD TO BLIZZ--
	SlashCmdList["AMBIENTAURA"] = function() InterfaceOptionsFrame_OpenToCategory("AmbientAura") end
	SLASH_AMBIENTAURA1 = "/ambientaura"
	SLASH_AMBIENTAURA2 = "/aa"
	config.name = select(2, GetAddOnInfo('AmbientAura'))
	InterfaceOptions_AddCategory(config)
	AddOptions()

	--INITIAL MODEL SETUP--
	AuraFrame:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT")
	AuraFrame:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT")
	AuraFrame:SetFrameStrata("BACKGROUND")
	if IsResting() then
		AuraFrame:SetModel("SPELLS\\GreenRadiationFog.m2")
	elseif InCombatLockdown() then
		AuraFrame:SetModel("SPELLS\\RedRadiationFog.m2")
	else
		AuraFrame:SetModel("SPELLS\\BlueRadiationFog.m2")
	end
end

AuraFrame:SetScript("OnEvent", function(self, event)
	if event == "PLAYER_LOGIN" then
		Initialize()
		SetUpModel()
		AuraFrame:UnregisterEvent("PLAYER_LOGIN")
	elseif event == "PLAYER_REGEN_DISABLED" then
		AuraFrame:SetModel("SPELLS\\RedRadiationFog.m2")
	elseif event == "PLAYER_REGEN_ENABLED" then
		AuraFrame:SetModel("SPELLS\\BlueRadiationFog.m2")
	elseif event == "PLAYER_UPDATE_RESTING" then
		if IsResting() then
			AuraFrame:SetModel("SPELLS\\GreenRadiationFog.m2")
		else
			AuraFrame:SetModel("SPELLS\\BlueRadiationFog.m2")
		end
	end
end)

AuraFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
AuraFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
AuraFrame:RegisterEvent("PLAYER_UPDATE_RESTING")
AuraFrame:RegisterEvent("PLAYER_LOGIN")